<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sets up a DateInterval from the relative parts of the string</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="dateinterval.construct.html">DateInterval::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="dateinterval.format.html">DateInterval::format</a></div>
 <div class="up"><a href="class.dateinterval.html">DateInterval</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="dateinterval.createfromdatestring" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateInterval::createFromDateString</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">DateInterval::createFromDateString</span> &mdash; <span class="dc-title">Sets up a DateInterval from the relative parts of the string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-dateinterval.createfromdatestring-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><strong>DateInterval::createFromDateString</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$time</code></span>
   )</div>

  <p class="para rdfs-comment">
   Uses the normal date parsers and sets up a DateInterval from the relative
   parts of the parsed string.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-dateinterval.createfromdatestring-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">time</code></em></dt>

     <dd>

      <p class="para">
       A date with relative parts. Specifically, the
       <a href="datetime.formats.relative.html" class="link">relative formats</a>
       supported by the parser used for <span class="function"><a href="function.strtotime.html" class="function">strtotime()</a></span> and
       <a href="class.datetime.html" class="classname">DateTime</a> will be used to construct the
       DateInterval.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-dateinterval.createfromdatestring-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2220">
    <p><strong>Example #1 Parsing valid date intervals</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Each&nbsp;set&nbsp;of&nbsp;intervals&nbsp;is&nbsp;equal.<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'1&nbsp;day'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P2W'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'2&nbsp;weeks'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P3M'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'3&nbsp;months'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P4Y'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'4&nbsp;years'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1Y1D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'1&nbsp;year&nbsp;+&nbsp;1&nbsp;day'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1DT12H'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'1&nbsp;day&nbsp;+&nbsp;12&nbsp;hours'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'PT3600S'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'3600&nbsp;seconds'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-dateinterval.createfromdatestring-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a new <a href="class.dateinterval.html" class="classname">DateInterval</a> instance.
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="dateinterval.construct.html">DateInterval::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="dateinterval.format.html">DateInterval::format</a></div>
 <div class="up"><a href="class.dateinterval.html">DateInterval</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
